angular.module('Controller')
.controller('WFMenuManageEditCtrl', ($scope,NgTableParams,menuManage,$localStorage,wfAlert,commonUtil,cryptUtil,commonConstant,$http, $uibModalInstance, items) ->
    type=items.type
    $scope.data=items.data
    $scope.modalTitle=if type=='new' then '新建菜单' else '编辑菜单'
    $scope.subMenuList=[]
    loadSubMenu=()->
        $scope.data.level=if $scope.data.parent<1 then 1 else 2
        $http.get('@ostengar/module/list?parent=0')
        .success((resp)->
            list=resp.message
            arr=[]
            _.each list,(item)->
                if item.isLeaf==false
                    arr.push {value:item.id,name:item.moduleName}
            $scope.subMenuList=arr
        )
    loadSubMenu()
    $scope.chooseLevel=()->
        if $scope.data.level>1 and $scope.data.parent==0 and $scope.subMenuList.length>0
            $scope.data.parent=$scope.subMenuList[0].value
        else
            $scope.data.parent=0
        console.log($scope.data.parent)
    $scope.modalMenuLvList=commonConstant.menuLvList()
    $scope.menuTypeList=commonConstant.menuTypeList()
    $scope.modalEnableList=commonConstant.enabledList()
    $scope.newData=()->
        postData={
            "accountId":$localStorage['accountId']
            "moduleName":$scope.data.moduleName
            "icon":$scope.data.icon
            "moduleUrl":$scope.data.moduleUrl
            "sortNo":$scope.data.sortNo
            "description":$scope.data.description
            "parent":if $scope.data.parent==undefined  then 0 else $scope.data.parent
            "parentName":$scope.data.level
            "isLeaf": $scope.data.isLeaf
            "enabled": $scope.data.enabled
            "cascadeId": "1"
        }
        $http.post('@ostengar/module',postData)
        .success((resp)->
            if resp.code == 0
                wfAlert.toastInfo($scope.modalTitle+'成功')
                $uibModalInstance.close('done')
            else
                wfAlert.toastError(resp.message)
        )
    $scope.editData=()->
        postData={
            "accountId":$localStorage['accountId']
            "moduleName":$scope.data.moduleName
            "icon":$scope.data.icon
            "moduleUrl":$scope.data.moduleUrl
            "sortNo":$scope.data.sortNo
            "description":$scope.data.description
            "parent":if $scope.data.parent==undefined  then 0 else $scope.data.parent
            "parentName":$scope.data.level
            "enabled": $scope.data.enabled
            "cascadeId": "1"
            "isLeaf": $scope.data.isLeaf
        }
        $http.put('@ostengar/module/'+$scope.data.id,postData)
        .success((resp)->
            if resp.code == 0
                wfAlert.toastInfo($scope.modalTitle+'成功')
                $uibModalInstance.close('done')
            else
                wfAlert.toastError(resp.message)
        )
    $scope.confirm = ()->
        if type=='new'
            $scope.newData()
        else
            $scope.editData()
    $scope.cancel =()->
        $uibModalInstance.dismiss('cancel')

)